package weixinkeji.dev.dc.base.scheme.combo.h.common;

import MyUI.ligerUI.bean.LigerUIOrderBy;
import MyUI.ligerUI.service.LigeruiService;
import com.alibaba.fastjson.JSON;
import sys.wx.ann.hm.H;
import sys.wx.ann.hm.M;
import sys.wx.c.ATR;
import sys.wx.c.components.cv.CV;
import sys.wx.m.db.SQL.mysql.common.TJF;
import sys.wx.m.db.SQL.mysql.common.where.Where;
import sys.wx.m.db.SQL.mysql.common.where.WhereConfig;
import sys.wx.m.db.dao.mysql.Dao;
import sys.wx.util.ToolService;
import weixinkeji.dev.dc.base.scheme.combo.bean.Combo;
import weixinkeji.dev.dc.base.scheme.combo.vo.ComboVO_Select;

/**
 *
 * @author IMY
 */
@H("view/show/combo")
public class ComboS {

    @M("/list") //view/show/combo/list.imy
    public static String list(ATR a) {
        String flid = CV.getParameter(a.request, "flid");
        LigerUIOrderBy orderBy = LigeruiService.getOrderBy(a.request);
        String ob = null;
        if (null != orderBy.sortname) {//字段排序
            ob = TJF.getOrderBy(null).put(orderBy.sortname).putASC_DESC(orderBy.sortorder).toSql();
        }

        if (null != flid) {//分类id不为空，按分类查询+字段排序
            a.printOne(JSON.toJSONString(Dao.selectVastOrderBy(Combo.class, Where.putIN(Where.TJ_IGNORE_NOTHING, null, "", "flid", CV.toSQLStringValue(flid)), ob)));
            return null;
        }

        //按条件查询查询
        ComboVO_Select vo_select = CV.getObjectByFields(ComboVO_Select.class, a.request);
        if (null != vo_select.andor) {
            a.printOne(JSON.toJSONString(Dao.selectVast(Combo.class, getVOSelectTJ(vo_select))));
            return null;
        }
//        System.out.println(vo_select.combo_code + "//" + vo_select.combo_name + "//" + vo_select.combo_style + "//" + vo_select.andor);
        //默认查询所有+字段排序
        a.printOne(JSON.toJSONString(Dao.selectVastOrderBy(Combo.class, ob)));
        return null;
    }

    private static String getVOSelectTJ(ComboVO_Select vo) {
        WhereConfig wc = new WhereConfig("");
        Where wh = new Where(wc);
        String and_or = vo.andor == 1 ? Where.AND : Where.OR;//1表示 AND; 0表示OR

//        System.out.println(vo.combo_style + "//" + ".....................................");
        wh.put(Where.TJ_IGNORE_EMPTY_NULL, and_or, "combo_style", "=", vo.combo_style);
//                if (null != vo.combo_style) {
//                    wh.putAND("combo_style", "=", vo.combo_style);
//                }
        if (!ToolService.IsNullOREmpty(vo.combo_name)) {
            wh.put(Where.TJ_IGNORE_NOTHING, and_or, "combo_name", "LIKE", "%" + vo.combo_name + "%");
//                    wh.putAND("combo_name", "LIKE", "%" + vo.combo_name + "%");
        }
        if (!ToolService.IsNullOREmpty(vo.combo_code)) {
            wh.put(Where.TJ_IGNORE_NOTHING, and_or, "combo_code", "LIKE", "%" + vo.combo_code + "%");
//                    wh.putAND("combo_code", "LIKE", "%" + vo.combo_code + "%");
        }
        return wh.getWhereAotuRemove0ANDOR();
//                String str = wh.getWhere();
//                return str.indexOf(Where.AND) == 0 ? str.substring(Where.AND.length()) : str;
    }

}
